// JUMPMAN buy now retailers
var JumpmanBuyNow = (function() {
    var defaults = {},
        settings;

    var shoeCount = shoeLoadedCount = 0;

    function getRetailers( opts ) {
        settings = $.extend( defaults, opts );

        for (product in settings.products) {
            logRetailers( settings.products[product] )
            shoeCount++;
        }
    }

    function logRetailers( product ) {
        //var keys = settings.webService.search(/\{([^}]+)\}/g,'x');
        var getUrl = settings.webService.replace('{colorId}', product['colorId']).replace('{styleId}', product['styleId']);

        //console.log(getUrl);
        $.ajax({
            url: getUrl,
            dataType: 'xml',
            type: 'GET',
            success: function( data ) {
                // if successful, but no results, display the buy now button
                if ($(data).find('message').text() != 'There were no results') {

                    product.retailers = data;

                } else {
                    // otherwise don't do anything
                };

                shoeLoadedCount++;

                checkLoadStatus();
            }
        });
    };

   function checkLoadStatus() {
        if (shoeCount === shoeLoadedCount) {
            if (settings.onLoadComplete && typeof settings.onLoadComplete === 'function') {
                settings.onLoadComplete.call( settings.products );
            }
        }
    };

    function getProductRetailers() {
        return settings.products;
    }

    return {
        getRetailers: getRetailers,
        getProductRetailers: getProductRetailers
    }
})();


